<?php

namespace App\Models;

use App\Http\Resources\Categories;
use App\Http\Resources\Coupons;
use App\Http\Resources\Images;
use App\Http\Resources\Skus;
use App\Http\Resources\Tags;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;
use Illuminate\Database\Eloquent\Builder;

class Product extends Model
{
    protected $casts = [
        'price' => 'array',
    ];

    const PRICE = [
        'current' => "现价: ",
        'original' => "原价: ",
        'wholesale' => "批发价",
    ];

    protected $with = ['coupons', 'cover'];

    protected $appends = ['wholesale', 'current', 'original'];

    public function category()
    {
        return $this->belongsTo(Category::class);
    }

    public function tags()
    {
        return $this->belongsToMany(Tag::class, 'product_tags');
    }

    public function sku()
    {
        return $this->hasMany(ProductSku::class);
    }

    public function cover()
    {
        return $this->hasOne(Image::class, 'type_id')
            ->where('type', Image::PRODUCT_COVER);
    }

    public function images()
    {
        return $this->hasMany(Image::class, 'type_id')
            ->where('type', Image::PRODUCT_IMAGES);
    }

    public function skus()
    {
        return $this->hasMany(ProductSkuAttribute::class);
    }

    public function attrs()
    {
        return $this->belongsToMany(Attribute::class, 'product_sku_attributes')
            ->distinct('attribute_id');
    }

    public function coupons()
    {
        return $this->belongsToMany(Coupon::class, 'product_coupons');
    }

    public function setCurrentAttribute($current)
    {
        $this->setAttribute('price->current', $current);
    }

    public function getCurrentAttribute()
    {
        return $this->price['current'];
    }

    public function setOriginalAttribute($original)
    {
        $this->setAttribute('price->original', $original);
    }

    public function getOriginalAttribute($original)
    {
        return $this->price['original'];
    }

    public function setWholesaleAttribute($wholesale)
    {
        $this->setAttribute('price->wholesale', $wholesale);
    }

    public function getWholesaleAttribute($wholesale)
    {
        return $this->price['wholesale'];
    }

    public function getAttrValueAttribute()
    {
        $return = [];
        foreach ($this->attrs as $attr) {
            $return[] = [
                'name' => $attr->name,
                'values' => ProductSkuAttribute::query()->where([
                    'product_id' => $this->id,
                    'attribute_id' => $attr->id
                ])->get()->pluck('attribute_value', 'id')
            ];
        }
        return $return;
    }

    public function display()
    {
        return [
            'id' => $this->id,
            'category' => Categories::make($this->category),
            'price' => $this->price,
            'name' => $this->name,
            'summary' => $this->summary,
            'tags' => Tags::collection($this->tags),
            'attributes' => $this->attr_value,
            'coupons' => Coupons::collection($this->coupons->merge($this->category->coupons)),
            'count_sales' => $this->count_sales,
            'images' => Images::collection($this->images),
            'sku' => Skus::collection($this->sku),
            'is_collection' => (int)isCollectionProduct($this->id), // 是否收藏
        ];
    }
}
